﻿using System;
using System.Web.Profile;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;


public partial class Members_MyAccountInfo : System.Web.UI.Page
{

    #region on page load get current profile

    protected void Page_Load(object sender, EventArgs e)
    {
        // Get Profile start....................................................
        if (!Page.IsPostBack)
        {
            if (Page.User.Identity.IsAuthenticated)
            {
                MembershipUser user = Membership.GetUser();
                SetActiveButtonAttributes(CancelButton);
                SetActiveButtonAttributes(ChangePasswordButton);
                SetActiveButtonAttributes(btnUpdateProfile);

                // get country names from app_code folder
                // bind country names to the dropdown list
                ddlCountries.DataSource = CountryNames.CountryNames.GetCountries();
                ddlCountries.DataBind();

                // get the selected user's profile based on query string
                ProfileCommon profile = Profile;


                // Personal Info
                txtDisplayName.Text = profile.Personal.DisplayName;
                txtEmail.Text = user.Email;
                ddlGenders.SelectedValue = profile.Personal.Gender;
                ddlOccupations.SelectedValue = profile.Personal.Occupation;
                txtWebsite.Text = profile.Personal.Website;
                txtTwitterName.Text = profile.Personal.TwitterName;
                ddlCountries.SelectedValue = profile.Personal.Country;

            }
        }
        // Get Profile end.......................................................
    }

    private void SetActiveButtonAttributes(Button button)
    {
        button.Attributes.CssStyle.Add("background-color", "#eee");
        button.Attributes.Add("OnMouseOut", "this.style.backgroundColor ='#eee';");
        button.Attributes.CssStyle.Add("text-decoration", "none");
        button.Attributes.CssStyle.Add("margin", "3px");
        button.Attributes.CssStyle.Add("padding", "7px 6px 7px 6px");
        button.Attributes.Add("OnMouseOver", "this.style.backgroundColor ='#ddd';");
    }

    #endregion

    #region Update current Profile Sub

    public void SaveProfile()
    {
        if (Page.User.Identity.IsAuthenticated)
        {
            // get the selected user's profile
            ProfileCommon profile = Profile;


            profile.Personal.DisplayName = txtDisplayName.Text;
            profile.Personal.Gender = ddlGenders.SelectedValue;
            profile.Personal.Occupation = ddlOccupations.SelectedValue;
            profile.Personal.Website = txtWebsite.Text;
            profile.Personal.Country = ddlCountries.SelectedValue;
            profile.Personal.TwitterName = txtTwitterName.Text;

            // this is what we will call from the button click
            // to save the user's profile
            profile.Save();

            MembershipUser user = Membership.GetUser();
            user.Email = txtEmail.Text;
            Membership.UpdateUser(user);

        }
    }

    #endregion

    #region Update current Profile Button Click

    protected void btnUpdateProfile_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            SaveProfile();
            lblProfileMessage.Text = "Profile saved successfully!";
        }
    }

    #endregion

    #region Sueetie Change Password

    protected void Cancel_Click(object sender, EventArgs e)
    {
        txtCurrentPassword.Text = string.Empty;
        txtNewPassword1.Text = string.Empty;
        txtNewPassword2.Text = string.Empty;
    }

    protected void ResetPassword_Click(object sender, EventArgs e)
    {

        MembershipUser user = Membership.GetUser();
        try
        {

            if (user.ChangePassword(txtCurrentPassword.Text, txtNewPassword1.Text))
            {
                ResultsText.Text = "Your password has been updated.";
            }
            else
            {
                if (user.IsLockedOut)
                    ResultsText.Text = "We're sorry.  You've exceeded the number of attempts to login and your account has been locked out.  Please <a href=\"mailto:admin@sueetie.org\">email us</a> to get immediate assistance.";
                else
                    ResultsText.Text = "The current password entered was not correct.  To retrieve your current password, please sign out and use our password retrieval services.</a>";
            }
        }
        catch (Exception exception)
        {
            ResultsText.Text = exception.Message;
        }

    }

    #endregion
}
